package com.zzb.servlet.company;

import com.alibaba.fastjson.JSONObject;
import com.zzb.DTO.ResultDTO;
import com.zzb.VO.CompanyVO;
import com.zzb.service.CompanyService;
import com.zzb.service.impl.CompanyServiceImpl;
import com.zzb.util.Tool;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

@WebServlet("/back/company/all")
public class CompanyFindAllServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //乱码处理
        req.setCharacterEncoding(StandardCharsets.UTF_8.toString());
        resp.setCharacterEncoding(StandardCharsets.UTF_8.toString());
        //得到前端数据
        String page = req.getParameter("page");
        String limit = req.getParameter("limit");
        String name = req.getParameter("name");
        String enable = req.getParameter("enable");
        String address = req.getParameter("address");
        String email = req.getParameter("email");


        //对前端数据进行封装
        CompanyVO companyVO = new CompanyVO();
        companyVO.setPage(Integer.valueOf(page));
        companyVO.setLimit(Integer.valueOf(limit));
        companyVO.setName(Tool.nullToStr(name));
        companyVO.setEnable(Integer.valueOf(Tool.nullToNum(enable)));
        companyVO.setAddress(Tool.nullToStr(address));
        companyVO.setEmail(Tool.nullToStr(email));


        //调用服务逻辑
        CompanyService companyService = new CompanyServiceImpl();

        //得到返回值
        ResultDTO resultDTO = companyService.findAll(companyVO);
        //响应
        resp.getWriter().println(JSONObject.toJSONString(resultDTO));
    }
}
